package com.wh.custom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.custom.dao.CustomMycustomMapper;
import com.wh.custom.mapper.EmployeeCustomerMapper;
import com.wh.org.dao.IEmployeeDao;
import com.wh.org.dto.EmployeeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.wh.custom.pojo.CustomReturnVisit;
import com.wh.custom.service.ICustomReturnVisitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wh.custom.mapper.CustomReturnVisitMapper;
import com.wh.custom.dto.CustomReturnVisitDto;

import java.security.cert.X509Certificate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class CustomReturnVisitServiceImpl implements ICustomReturnVisitService {

    @Autowired
    private CustomReturnVisitMapper customReturnVisitMapper;
    @Autowired
    private IEmployeeDao iEmployeeDao;
    @Autowired
    private CustomMycustomMapper customMycustomMapper;

    @Override
    public void add(CustomReturnVisit customReturnVisit){
        customReturnVisitMapper.insert(customReturnVisit);
    }

    @Override
    public void deleteById(Long id){
        customReturnVisitMapper.deleteById(id);
    }

    @Transactional
    @Override
    public void patchDel(List<Long> ids){
        customReturnVisitMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(CustomReturnVisit customReturnVisit){
        customReturnVisitMapper.updateById(customReturnVisit);
    }

    @Override
    public CustomReturnVisit findOne(Long id){
        return customReturnVisitMapper.selectById(id);
    }

    @Override
    public List<CustomReturnVisit>findAll(){
        return customReturnVisitMapper.selectList(null);
    }

    @Override
    public IPage<CustomReturnVisit>findByPage(CustomReturnVisitDto customReturnVisitDto){
        //1.创建查询条件
        QueryWrapper<CustomReturnVisit> qw = new QueryWrapper<>();
        //qw.like("xxx",customReturnVisitDto.getXxx());
        //qw.or();
        //qw.like("xxx",customReturnVisitDto.getXxx());
        //2.组件分页数据
        IPage<CustomReturnVisit> page = new Page<>(customReturnVisitDto.getCurrentPage(), customReturnVisitDto.getPageSize());
        page.setRecords(customReturnVisitMapper.findByPage(page,qw));
        page.setTotal(customReturnVisitMapper.selectCount(qw));
        //3.返回
        return page;
    }

    @Override
    public void addFollow(CustomReturnVisit customReturnVisit) {
        Long customId = customReturnVisit.getCustomId();
        customReturnVisit.setCustomId(customId);
        LocalDateTime dateTime = LocalDateTime.now();
        customReturnVisit.setDate(dateTime);
        Long loginInfoId = customReturnVisit.getLoginInfoId();

        Long idbyLogininfoId = iEmployeeDao.findIdbyLogininfoId(loginInfoId);
        customReturnVisit.setRecordType(0);
        customReturnVisit.setOperator(idbyLogininfoId);//存储登录人Id
        customReturnVisitMapper.insert(customReturnVisit);
        //更新客户的最近修改时间
        customMycustomMapper.updateTime(dateTime,customId);
    }

    @Override
    public List<CustomReturnVisit> customerAllFollowUp(Integer customerId) {
        return customReturnVisitMapper.customerAllFollowUp(customerId);
    }
}
